<?php
/**
 * Created by JetBrains PhpStorm.
 * User: thinhhung
 * Date: 5/31/12
 * Time: 5:08 PM
 * To change this template use File | Settings | File Templates.
 */
class ChiTietBansController extends AppController
{
    public $helper = array('Html', 'Form', 'Ajax');
    public $name = 'ChiTietBans';
//var $helpers = array('Ajax', 'Javascript');
    var $components = array('Session');

    var $paginate = array(
        'limit' => '7',
        'order' => array(
            'chitietban.id' => 'asc'
        )
    );


    public function index($id = null)
    {
        $this->set('title_for_layout', 'Chi tiết hàng hóa bán');
        $MAHDB = $this->Session->read('MAHDB');
        $loaihd = $this->ChiTietBan->HoaDonBan->findById($MAHDB);
        //$this->Session->setFlash($loaihd['HoaDonBan']['LOAIHD']);
        //print_r($loaihd);

        if ($loaihd['HoaDonBan']['LOAIHD'] == null) {
            //  $this->Session->setFlash('0');
        } else {
            // $this->Session->setFlash('1');
        }


        //$gia = $this->ChiTietBan->SanPham->findById(1);
        // $this->set('gia', $gia);

        //$this->autoRender = false;
        //$this->layout = 'ajax';

        //$this->Session->setFlash($this->request->data);
        //$gia = $this->ChiTietBan->SanPham->find("list", array('fields' => array('SanPham.id', 'SanPham.GIABANLE', 'SanPham.GIABANSI', 'SanPham.GIAUUDAI')));
        // $this->set('gias', $gia);

        /*$sql = 'select * from HoaDonNhap where MAHDBhap =' . '"' . $MAHDB . '"';
        $result = $this->Nhap->query($sql);
        $nvien = $this->Session->read('manhanvien');
        $mode = $this->Session->read('mode');
        */

        // if ($mode != 'admin') {
        //    if ($result[0]['HoaDonNhap']['MaNV'] != $nvien) {
        //        $this->redirect(array('controller' => '', 'action' => 'index'));
        //    }
        //  }
        //$this->Session->write('MAHDB', $MAHDB);
        //$this->Session->setFlash($MAHDB);
        //$a = $MAHDB;

        //$this->set('hanghoas', $this->HangHoa->find('all'));
        //$bans=$this->Ban->find('all', array('conditions'=>array('Ban.MaHDBan' => $a)));
        $this->ChiTietBan->recursive = 0;
        // $this->set('bans', $this->paginate($conditions => array('Ban.MaHDBan' => $a)));
        $bans = $this->paginate('ChiTietBan', array('ChiTietBan.MAHDB =' => $id));
        $this->set('bans', $bans);
        //$temps = $MAHDB;
        //$this->set('MAHDB', $id);
        //$this->Session->write('MAHDB', $id);
    }


    public function view($id = null)
    {
        $this->Nhap->id = $id;
        $this->set('ban', $this->Nhap->Read());
    }

    public function add()
    {
        //$this->layout = 'ajax';
        //$this->autoRender = false;
        //$gia = $this->ChiTietBan->SanPham->find("list", array('fields' => array('SanPham.id', 'SanPham.GIABANLE', 'SanPham.GIABANSI', 'SanPham.GIAUUDAI')));


        if ($this->request->is('post')) {
            $this->set('title_for_layout', 'Thêm hàng hóa vào hóa đơn');

            $sql_hdbmoinhat = 'select max(id) as moinhat from hoadonban';
            $resutl_hdbmoinhat = $this->HoaDonBan->query($sql_hdbmoinhat);

            $mahdbmoi = $resutl_hdbmoinhat['0']['0']['moinhat'] +1;

            $sql_themhoadonban = 'insert into hoadonban(id) values("' . $mahdbmoi . '")';
            $result_themhoadonban = $this->ChiTietBan

            $MAHDB = $this->Session->read('MAHDB');
            $loaihd = $this->ChiTietBan->HoaDonBan->findById($MAHDB);

            $sanphamban = $this->ChiTietBan->SanPham->findById($this->request->data['ChiTietBan']['MASP']);

            if ($loaihd['HoaDonBan']['LOAIHD'] == null) {
                //$this->Session->setFlash('le');
                $this->request->data['ChiTietBan']['DONGIABAN'] = $sanphamban['SanPham']['GIABANLE'];
                // $this->request->data['ChiTietBan']['DONGIABAN'] = 999;
            } else {
                //$this->Session->setFlash('si');
                $this->request->data['ChiTietBan']['DONGIABAN'] = $sanphamban['SanPham']['GIABANSI'];
                // $this->request->data['ChiTietBan']['DONGIABAN'] = 888;
            }
            if ($this->ChiTietBan->save($this->request->data)) {
                $this->Session->setFlash('Đã thêm hàng hóa vào danh sách');
                //$this->Session->setFlash('Đã thêm hàng hóa vào danh sách');
                //$this->Session->setFlash($this->request->data['ChiTietBan']['SLBAN']);
                //print_r($this->data);

                $masp = $this->request->data['ChiTietBan']['MASP'];
                $soluong = $this->request->data['ChiTietBan']['SLBAN'];
                $sql_banhang = 'update sanpham set soluong = soluong - ' . $soluong . ' where masp = ' . $masp;
                $this->ChiTietBan->query($sql_banhang);

                $MAHDB = $this->Session->read('MAHDB');
                $this->redirect(array('controller' => 'chi_tiet_bans', 'action' => 'index', $MAHDB));

            }
        }
    }

    public function add_()
    {
        $this->layout = 'ajax';
        $this->autoRender = false;
        if (!empty($this->data)) {
            $this->ChiTietBan->create();
            if ($this->ChiTietBan->save($this->data)) {
                $this->Session->setFlash('Đã thêm hàng hóa vào danh sách');
                $this->redirect($this->referer());
            }
        }
    }


    public function edit($id = null)
    {
        /*  $sql = 'select * from HoaDonNhap where MAHDBhap =' . '"' . $MAHDB . '"';
       $result = $this->Nhap->query($sql);
       $nvien = $this->Session->read('manhanvien');
       $mode = $this->Session->read('mode');
       if ($mode != 'admin') {
           if ($result[0]['HoaDonNhap']['MaNV'] != $nvien) {
               //$this->redirect(array('controller' => '', 'action' => 'index'));

           }
       } */
        if ($this->Session->read('quyen') != 'nhanvien') {
            $this->set('title_for_layout', 'Cập nhật thông tin hàng hóa trong hóa đơn');
            $this->ChiTietBan->id = $id;
            if ($this->request->is('get')) {
                $this->request->data = $this->ChiTietBan->read();

                //tim so luong truoc khi cap nhat
                $sql_laysoluong = 'select * from ChiTietBan as ChiTietBan where masp = ' . $this->request->data['ChiTietBan']['MASP']
                    . ' and MAHDB = ' . $this->request->data['ChiTietBan']['MAHDB'];

                $kqtruocCN = $this->ChiTietBan->query($sql_laysoluong);
                $this->Session->write('ban_truoc', intval($kqtruocCN['0']['ChiTietBan']['SLBAN']));

            } else {
                if ($this->ChiTietBan->save($this->request->data)) {
                    // intval() : string to int
                    //tim so luong sau khi cap nhat, cap nhat lai so luong chenh lech
                    $sql_laysoluong = 'select * from ChiTietBan as ChiTietBan where masp = ' . $this->request->data['ChiTietBan']['MASP']
                        . ' and MAHDB = ' . $this->request->data['ChiTietBan']['MAHDB'];
                    $kqsauCN = $this->ChiTietBan->query($sql_laysoluong);
                    intval($kqsauCN['0']['ChiTietBan']['SLBAN']);

                    $soluongmoi = intval($kqsauCN['0']['ChiTietBan']['SLBAN']) - $this->Session->read('ban_truoc');
                    $sql_nhaphangvaokho = 'update sanpham set soluong = soluong + ' . $soluongmoi . ' where masp = ' . $this->request->data['ChiTietBan']['MASP'];
                    $this->ChiTietBan->query($sql_nhaphangvaokho);

                    $this->Session->setFlash('Thông tin hàng hóa đã được cập nhật');
                    $this->redirect(array('action' => 'index', $this->Session->read('MAHDB')));
                }
            }
        } else {
            $this->redirect(array('controller' => '', 'action' => 'index'));
        }
    }

    public function delete($id)
    {
        /* $sql = 'select * from HoaDonNhap where MAHDBhap =' . '"' . $MAHDB . '"';
       $result = $this->ChiTietBan->query($sql);
       $nvien = $this->Session->read('manhanvien');
       $mode = $this->Session->read('mode');
       if ($mode != 'admin') {
           if ($result[0]['HoaDonNhap']['MaNV'] != $nvien) {
               $this->redirect(array('controller' => '', 'action' => 'index'));
           }
       } */

        if ($this->Session->read('quyen') == 'quantri') {
            if (!$this->request->is('post')) {
                throw new MethodNotAllowedException();
            } else {
                $dl_xoa = $this->ChiTietBan->findAllByid($id);
                print_r($dl_xoa);
                $sql_laysoluong = 'select * from ChiTietBan as ChiTietBan where masp = ' . $dl_xoa['0']['ChiTietBan']['MASP']
                    . ' and MAHDB = ' . $dl_xoa['0']['ChiTietBan']['MAHDB'];
                $slseDEL = $this->ChiTietBan->query($sql_laysoluong);
                //$this->Session->setFlash($slseDEL);
                $sql_xoahd = 'update sanpham set soluong = soluong + ' . intval($slseDEL['0']['ChiTietBan']['SLBAN']) . ' where masp = ' . $dl_xoa['0']['ChiTietBan']['MASP'];
                $this->ChiTietBan->query($sql_xoahd);

                if ($this->ChiTietBan->delete($id)) {


                    //$this->Session->write('nhap_truoc', intval($kqtruocCN['0']['ChiTietBan']['SLBAN']));

                    $this->Session->setFlash('Đã xóa hàng hóa ra khỏi danh sách');
                    //$this->redirect(array('action' => 'index'));
                    $this->redirect($this->referer());
                }
            }
        } else {
            $this->redirect(array('controller' => '', 'action' => 'index'));
        }
    }
}